package xyz.scootaloo.oj.leetcode;

/**
 * 暴力法
 * @author flutterdash@qq.com
 * @since 2021/3/30 14:21
 */
public class P16a {

    public int threeSumClosest(int[] nums, int target) {
        int closeNumber = nums[0] + nums[1] + nums[2];
        int av = getAbsoluteValue(target, closeNumber);
        int len = nums.length;
        for (int i = 0; i<len; i++) {
            for (int j = (i + 1); j<len; j++) {
                for (int k = (j + 1); k<len; k++) {
                    int currentNumber = nums[i] + nums[j] + nums[k];
                    int currentAv = getAbsoluteValue(target, currentNumber);
                    if (currentAv < av) {
                        av = currentAv;
                        closeNumber = currentNumber;
                    }
                }
            }
        }

        return closeNumber;
    }

    private int getAbsoluteValue(int num1, int num2) {
        if (num1 > num2) {
            return num1 - num2;
        } else {
            return num2 - num1;
        }
    }

    public static void main(String[] args) {
        int[] input_1 = {-1, 2, 1, -4};
        int target_1 = 1;
        P16a p16A = new P16a();
        System.out.println(p16A.threeSumClosest(input_1, target_1));
    }

}
